package com.ys.day31.p3FastJson;

import com.alibaba.fastjson.JSONArray;
import com.google.gson.JsonArray;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.List;

/**
 * 描述:  请求远程json 转 Java 对象 输出
 *
 * @author CodeYang_Site
 * @version 2021/1/22 1:03
 */
public class P3FastJson_RequsetServer {
    private static final String URL_PATH = "https://douhuvillage.cn/json/json.json";

    public static void main(String[] args) {
        try {
            String jsonData = JSONUtils.downJsonData(URL_PATH);
            System.out.println(jsonData);
            // JSONArray 转 json数组 为 java 数组
            List<Student> studentList = JSONArray.parseArray(jsonData, Student.class);
            for (Student student : studentList) {
                System.out.println(student);
            }
            // 在把 集合 转为 str
            String convertJson = JSONArray.toJSONString(studentList);
            System.out.println(convertJson);

            // 在把json 持久化到 磁盘上
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("day_31/etc/jsonArr.json")));
            out.write(convertJson);
            out.newLine();
            out.flush();
            out.close();
            System.out.println("持久化成功");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
